//
//  VisitorLogView.swift
//  mySina
//
//  Created by mac on 15/12/17.
//  Copyright © 2015年 tz. All rights reserved.
//

import UIKit

//定义协议
@objc protocol VisitorLogViewDelegate:NSObjectProtocol {
    //用户将要注册
    optional func userWillRegister()
    //用户将要登陆
    optional func userWillLogin()
}

class VisitorLogView: UIView {
    //声明代理
    weak var visitorLogViewDelegate:VisitorLogViewDelegate?
    //对外提供设置访客视图消息的方法
    func setupInfo(title title:String, imageName:String?) {
        //设置不同界面下得标题文字
        label.text = title
        if let name = imageName {
            //应为首页的imageName == nil ，所以到这里就说明不是首页
            //隐藏大房子
            largeIcon.hidden = true
            //应为大房子图片已经隐藏，后面的界面要想展示视图，需要从大圆圈图片下手
            smallicon.image = UIImage(named: name)
            //将图层移到最上面
            bringSubviewToFront(smallicon)
        } else {
            //如果是首页，开启圆圈图片的动画
            startAnimation()
        }
    }
    private func startAnimation() {
        let anima = CABasicAnimation(keyPath: "transform.rotation")
        //设置动画属性
        anima.duration = 20.0
        anima.repeatCount = MAXFLOAT
        anima.toValue = M_PI * 2
        //当动画完毕或者失去活跃状态，不移除图层
        anima.removedOnCompletion = false
        //添加动画
        smallicon.layer.addAnimation(anima, forKey: nil)
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    private func setupUI() {
        //添加子控件的时候，可以决定图层的分布
        addSubview(smallicon)
        addSubview(backIcon)
        addSubview(largeIcon)
        addSubview(label)
        addSubview(registerBtn)
        addSubview(loginBtn)
        //子视图的自动本局
        for subview in subviews {
            //只有将次方法设置为false，自动布局才有效果
            //在使用手码添加约束 需要屏蔽 frame布局
            subview.translatesAutoresizingMaskIntoConstraints = false
        }
        //大房子的自动布局
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterX, relatedBy: .Equal, toItem: self, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: largeIcon, attribute: .CenterY, relatedBy: .Equal, toItem: self, attribute: .CenterY, multiplier: 1, constant: -60))
        //大圆圈的自动布局
        addConstraint(NSLayoutConstraint(item: smallicon, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: smallicon, attribute: .CenterY, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterY, multiplier: 1, constant: 0))
        //说明文本的自动布局
        addConstraint(NSLayoutConstraint(item: label, attribute: .Top, relatedBy: .Equal, toItem: smallicon, attribute: .Bottom, multiplier: 1, constant: 20))
        addConstraint(NSLayoutConstraint(item: label, attribute: .CenterX, relatedBy: .Equal, toItem: largeIcon, attribute: .CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: label, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 230))
        //注册按钮的添加
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Right, relatedBy: .Equal, toItem: label, attribute: .Right, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: registerBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        //登录按钮
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Left, relatedBy: .Equal, toItem: label, attribute: .Left, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Top, relatedBy: .Equal, toItem: label, attribute: .Bottom, multiplier: 1, constant: 10))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Width, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 100))
        addConstraint(NSLayoutConstraint(item: loginBtn, attribute: .Height, relatedBy: .Equal, toItem: nil, attribute: .NotAnAttribute, multiplier: 1, constant: 35))
        //遮罩
        //水平方向上的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[backIcon]-0-|", options: [], metrics: nil, views: ["backIcon":backIcon]))
        //垂直方向上的约束
        addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[backIcon]-(-35)-[registerBtn]", options: [], metrics: nil, views: ["backIcon":backIcon,"registerBtn":registerBtn]))
        //设置视图的背景颜色
        backgroundColor = UIColor(white: 0.93, alpha: 1)
        //MARK: 登陆按钮和注册按钮的点击事件
        //注册按钮的点击事件
        registerBtn.addTarget(self, action: "registerBtnDidClick", forControlEvents: .TouchUpInside)
        //登陆按钮的点击事件
        loginBtn.addTarget(self, action: "loginBtnDidClick", forControlEvents: .TouchUpInside)
    }
    //MARK: 注册按钮和登陆按钮的监听
    @objc private func registerBtnDidClick() {
        //代理调用协议方法
        visitorLogViewDelegate?.userWillRegister?()
    }
    @objc private func loginBtnDidClick() {
        visitorLogViewDelegate?.userWillLogin?()
    }
    //懒加载所有的子视图
    //大房子
    private lazy var largeIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    //大圆圈
    private lazy var smallicon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    //遮罩
    private lazy var backIcon:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"));
    //说明文本
    private lazy var label:UILabel = UILabel(title: "关注一些人，回这里看看有什么惊喜", color: UIColor.darkGrayColor(), fontSize: 14.0)
    //注册按钮
    private lazy var registerBtn:UIButton = UIButton(title: "注册", backImageName: "common_button_white_disable", fontSize: 14.0, color: themeColor)
    //登录按钮
    private lazy var loginBtn:UIButton = UIButton(title: "登陆", backImageName: "common_button_white_disable", fontSize: 14.0, color: themeColor)
}